/*
 * @Author: master
 * @Date: 2021-12-02 14:02:03
 * @LastEditTime: 2022-04-13 15:51:59
 * @Description: 日期、时间服务
 */
import moment from 'moment';
import momentTz from 'moment-timezone';
/**
 * 获取当前时间
 */
export const handleGetNow = () => {
  /**
   * 数字不满两位前面加0
   */
  const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : '0' + n;
  };
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${formatNumber(month)}-${formatNumber(day)} ${formatNumber(hour)}:${formatNumber(
    minute,
  )}:${formatNumber(second)}`;
};
/**
 * 获取上一月
 */
export const getPreMonth = date => {
  let [year, month] = [date.split('-')[0], date.split('-')[1]];
  month = parseInt(month) - 1;
  if (month === 0) {
    year = parseInt(year) - 1;
    month = 12;
  }
  if (month < 10) {
    month = '0' + month;
  }
  return year + '-' + month;
};
/**
 * 获取下一月
 */
export const getNextMonth = date => {
  let [year, month] = [date.split('-')[0], date.split('-')[1]];
  if (month === 12) {
    month = 1;
    year = parseInt(year) + 1;
  } else {
    month = parseInt(month) + 1;
    if (month > 12) {
      month = 1;
      year = parseInt(year) + 1;
    }
  }
  if (month < 10) {
    month = '0' + month;
  }
  return year + '-' + month;
};
/**
 * 获取当前月的起始日
 */
export const getNowStartDay = () => {
  const date = new Date();
  let month = date.getMonth() + 1;
  if (month < 10) {
    month = '0' + month;
  }
  return date.getFullYear() + '-' + month + '-' + '01';
};
/**
 * 获取当前月的截止日
 */
export const getNowEndDay = () => {
  const date = new Date();
  let month = date.getMonth() + 1;
  if (month < 10) {
    month = '0' + month;
  }
  date.setMonth(month);
  date.setDate(0);
  const day = date.getDate();
  return date.getFullYear() + '-' + month + '-' + day;
};
/**
 * 根据日期获取当前月的起始日
 */
export const getNowStartDayByDate = date => {
  return date + '-' + '01';
};
/**
 * 根据日期获取当前月的截止日
 */
export const getNowEndDayByDate = date => {
  const dateSplit = date.split('-');
  const newDate = new Date(dateSplit[0], dateSplit[1]);
  let month = newDate.getMonth();
  if (month < 10 && month > 0) {
    month = '0' + month;
  }
  newDate.setMonth(month);
  newDate.setDate(0);
  const day = newDate.getDate();
  if (month === 0) {
    month = 12;
  }
  return newDate.getFullYear() + '-' + month + '-' + day;
};
/**
 * 根据日期获取当前月的时间戳
 */
export const getNowTimeStamp = date => {
  const dateSplit = date.split('-');
  return new Date(dateSplit[0], parseInt(dateSplit[1]) - 1);
};
/**
 * 获取当前年月日{year,month,day}
 */
export const getCurrentYearMonthDay = currentDate => {
  if (!currentDate) {
    return '';
  }
  if (typeof currentDate === 'string') {
    return currentDate;
  }
  let month = currentDate.getMonth() + 1;
  let day = currentDate.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  return { currentYear: currentDate.getFullYear(), currentMonth: month, currentDay: day };
};
/**
 * 获取当前日期格式信息20180101
 */
export const getCurrentDateString = currentDate => {
  if (!currentDate) {
    return '';
  }
  if (typeof currentDate === 'string') {
    return currentDate;
  }
  let month = currentDate.getMonth() + 1;
  let day = currentDate.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  return currentDate.getFullYear() + `${month}` + day;
};
/**
 * 获取当前日期格式信息yyyy-MM-dd HH:mm:ss
 */
export const getCurrentTimeString = currentDate => {
  if (!currentDate) {
    return '';
  }
  if (typeof currentDate === 'string') {
    return currentDate;
  }
  let month = currentDate.getMonth() + 1;
  let day = currentDate.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  let hour = currentDate.getHours();
  let min = currentDate.getMinutes();
  let sec = currentDate.getSeconds();
  hour = hour < 10 ? '0' + hour : hour;
  min = min < 10 ? '0' + min : min;
  sec = sec < 10 ? '0' + sec : sec;
  const dayN = hour + ':' + min + ':' + sec;
  return currentDate.getFullYear() + '-' + month + '-' + day + ' ' + dayN;
};
/**
 * 获取最近一年
 */
export const getLatelyMonth = CurrentMonth => {
  let [year, month] = [CurrentMonth.split('-')[0], CurrentMonth.split('-')[1]];
  month = parseInt(month) - 12;
  if (month === 0) {
    month = 12;
    year--;
  } else if (month < 0) {
    month = 12 + month;
    year--;
  }
  if (month < 10) {
    month = '0' + month;
  }
  return {
    start: year + '' + month,
    end: CurrentMonth.split('-')[0] + '' + CurrentMonth.split('-')[1],
  };
};
/**
 * 获取当前月份的天数
 */
export const getCurrentDays = CurrentMonth => {
  const year = CurrentMonth.split('-')[0];
  const month = parseInt(CurrentMonth.split('-')[1]);
  const temp = new Date(year, month, 1);
  return new Date(temp.getTime() - 864e5).getDate();
};
/**
 * 计算一周的开始
 */
export const handleGetWeekStart = (date = '') => {
  // 计算当天是周几
  const weekOfDay = Number(moment(date).format('E'));
  // 周一的日期
  return moment(date)
    .subtract(weekOfDay - 1, 'days')
    .format('YYYY-MM-DD');
};
/**
 * 计算一周的结束为周天
 */
export const handleGetWeekEnd = (date = '') => {
  // 计算当天是周几
  const weekOfDay = Number(moment(date).format('E'));
  // 周天的日期
  return moment(date)
    .add(7 - weekOfDay, 'days')
    .format('YYYY-MM-DD');
};
/**
 * 日期时间格式转换
 */
export const renderTime = date => {
  const dateee = new Date(date).toJSON();
  return new Date(+new Date(dateee) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/T/g, ' ')
    .replace(/\.[\d]{3}Z/, '');
};
/**
 * 获取当前日期格式信息2018-01-01
 */
export const getCurrentDate = currentDate => {
  if (!currentDate) {
    return '';
  }
  if (typeof currentDate === 'string') {
    return currentDate;
  }
  let month = currentDate.getMonth() + 1;
  let day = currentDate.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  return currentDate.getFullYear() + '-' + month + '-' + day;
};
/**
 * 时间格式转换
 * @param value //秒
 */
export const formatSeconds = value => {
  let theTime = parseInt(value, 0); // 秒
  if (isNaN(theTime)) {
    return false;
  }
  let theTime1 = 0; // 分
  let theTime2 = 0; // 小时
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60, 0);
    theTime = parseInt(theTime % 60, 0);
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60, 0);
      theTime1 = parseInt(theTime1 % 60, 0);
    }
  }
  // let result = ""+parseInt(theTime,0)+"秒";
  let result = '';
  if (theTime1 > 0) {
    result = '' + parseInt(theTime1, 0) + '分' + result;
  }
  if (theTime2 > 0) {
    result = '' + parseInt(theTime2, 0) + '小时' + result;
  }
  return result;
};
/**
 * 本地时间转utc时间
 * @param {*} dateTimeString string时间
 * @param {*} format 时间format格式
 * @returns utc 世界标准时间
 */
export const localTimeToUtc = (dateString, format = 'YYYY-MM-DD HH:mm:ss') => {
  const localTime = moment(new Date(dateString), format);
  return moment.utc(localTime).format();
};
/* 
   使用demo
   import moment from 'moment';
   import { localTimeToUtc, utcToLocalTime } from '@/common/tool/date';
   const selectedDate = moment(new Date(), 'YYYY-MM-DD HH:mm:ss');
   const utc = localTimeToUtc(selectedDate);
   console.warn('utc', utc);
   const localTime = utcToLocalTime(utc);
   console.warn('本地时间', localTime);
*/
/**
 * utc时间转本地时间
 * @param {*} utc 世界标准时间
 * @param {*} format 时间format格式
 * @returns dateTimeString
 */
export const utcToLocalTime = (utc, format = 'YYYY-MM-DD HH:mm:ss') => {
  return moment.utc(utc).local().format(format);
};
/**
 * utc时间转自定义时区时间
 * @param {Object} utc 世界标准时间
 * @param {String} tz Asia/Shanghai
 * @param {*} format 时间format格式
 * 
   import moment from 'moment';
   import { utcToTimeZones, localTimeToUtc } from '@/common/tool/date.js';
   // 本地时间（string）
   const selectedDate = moment(new Date(), 'YYYY-MM-DD HH:mm:ss');
   // 转换成utc时间
   const utc = localTimeToUtc(selectedDate);
   // utc时间转成其它时区时间（美国时间）
   const time = utcToTimeZones(utc, 'America/New_York');
 */
export const utcToTimeZones = (utc, tz = 'Asia/Shanghai', format = 'YYYY-MM-DD HH:mm:ss') => {
  try {
    // utc时间转本地moment
    const localMoment = momentTz.utc(utc).local();
    // 转成当地时间moment
    const tzMoment = localMoment.clone().tz(tz);
    return tzMoment.format(format);
  } catch (e) {
    console.error(e);
  }
};
